/**
 * Created by zqk on 2018/12/11.
 */
var form,
    taskType = xhTool.getQuery('iType'),
    iTTId = xhTool.getQuery('iTTId'),
    ifFirst = 1
$(document).ready(function(){
    var form = new xhForm($("#xh_form"));
    form.parse();
    if ($('.xh-area-button').length > 0) {
        $('.xh-area-button').append('<font style="color: red;margin-left: 20px;">注意:&nbsp;&nbsp;黄框为必填项，请检查是否填写</font>');
    }
    var appPackDom = $('#XHTrack_Task_sAppPackage');
    if(appPackDom.length > 0) {
        var appArr = [{id:'', text:''}];
        $.each(useful.appArr, function(id, value){
            appArr.push({id: id, text: value});
        });
        appPackDom.select2({
            placeholder: '选择产品',
            data: appArr
        });
    }

    //问题分类
    var proStyle = $('#sProblemStyle');
    if (taskType == 2) {
        if(proStyle.length > 0){
            proStyle.combotree({
                valueField:'id',
                textField:'text',
                editable:true,
                url:'getSearchEnum',
                // width:400
            });
        }
    } else {
        if (proStyle.length > 0) {
            $('#sProblemStyle').parent().parent().hide();
        }
    }
    //上报人
    var iReportUserId = $('#iReportUserId');
    if(taskType==2){
        if(iReportUserId.length > 0){
            iReportUserId.combobox({
                valueField:'id',
                textField:'text',
                url:'getReportUserId',
                // width:400,
            });
        }
    }else{
        if(iReportUserId.length > 0){
            $('#iReportUserId').parent().parent().hide();
        }
    }
    if (!iTTId) {
        $('.newTask').show();
        //指派人
        var assignUserId = $('#assignUserId');
        if(taskType==2){
            if(assignUserId.length > 0){
                assignUserId.combobox({
                    valueField:'id',
                    textField:'text',
                    url:'getAssignUserId',
                    // width:400,
                });
            }
        }else{
            if(assignUserId.length > 0){
                $('#assignUserId').parent().parent().hide();
            }
        }
    }



    if(taskType!=2){
        $('#iProLevel').parent().parent().hide();
    }

    var schoolArr = [];
    $.each(useful.schoolArr, function(id, name){
        schoolArr.push({id: id, text: name});
    });
    $('#XHTrack_Task_sSchoolId').select2({
        placeholder: '选择学校',
        data: schoolArr,
        allowClear: true
    });
    $('#XHTrack_Task_sMemberId').select2({
        placeholder: '提问者',
        data: [],
        allowClear: true
    });

    form.bindRequired($('#bt_form_add, #bt_form_edit'));

    $('#XHTrack_Task_sSchoolId').on('change', function () {
        var schoolIds = $('#XHTrack_Task_sSchoolId').val();
        if (!schoolIds) {
            $("#XHTrack_Task_sMemberId").html('')
            $("#XHTrack_Task_sMemberId").select2("val", " ");
        } else {
            $.ajax({
                url: 'getSchoolMember',
                data: {schoolIds: schoolIds},
                type: 'POST',
                success: function (data) {
                    $("#XHTrack_Task_sMemberId").select2("val", " ");
                    $("#XHTrack_Task_sMemberId").html('')
                    $('#XHTrack_Task_sMemberId').select2({
                        placeholder: '提问者',
                        data: data,
                        allowClear: true
                    });
                    if (ifFirst && iTTId) {
                        ifFirst = 0;
                        $.post(baseUrl + 'Track/getTaskInfo', {id:iTTId}, function(data){
                            $('#XHTrack_Task_sMemberId').val(data.sOfferIds ? data.sOfferIds.split(',') : '').trigger('change');//.prop("disabled", true);
                        }, 'json');
                    }
                },
                error: function (data) {
                    window.top.xhTop.showError(
                        data.responseJSON.error,
                        data.responseJSON.msg
                    )
                }
            })
        }

    });


    /** 表单 **/
    $('#bt_form_add').click(function(){
        xhExtra.setBtnLoading($('#bt_form_add'));
        $('#XHTrack_Task_iTaskType').val(taskType);
        form.submit('track/modifyTrackTaskData',{},function (res) {
            if (res.code == 200) {
                window.top.xhTop.showSuccess('新增成功');
                setTimeout(function () {
                    if (taskType == 2) {
                        var tabName = '问题反馈';
                    } else if (taskType == 1) {
                        var tabName = '需求管理';
                    }
                    window.top.xhTop.getFrameByName(tabName).list.datagrid('reload');
                    window.top.xhTop.closeActiveTab();
                },100);
            }
        },{check:false});

    });
    $('#bt_form_edit').click(function(){
        xhExtra.setBtnLoading($('#bt_form_edit'));
        form.submit('track/modifyTrackTaskData',{},function (res) {
            if (res.code == 200) {
                window.top.xhTop.showSuccess('编辑成功');
                setTimeout(function () {
                    if (taskType == 2) {
                        var tabName = '问题反馈';
                    } else if (taskType == 1) {
                        var tabName = '需求管理';
                    }
                    window.top.xhTop.getFrameByName(tabName).list.datagrid('reload');
                    window.top.xhTop.closeActiveTab();
                },1000);

            }
        },{check:false});
    });

    //表单返回按钮
    $('#bt_form_back').click(function(){
        window.top.xhTop.closeActiveTab();
    });
    $('#bt_form_reset').click(function(){
        $('#bt_form_add, #bt_form_edit').setButtonDisable('bs');
        $('.xh-form').resetForm().parseForm();
    });


    if(taskType ==2){
        var um = UE.getEditor('XHTrack_Problem_sTaskContent', {
            autoHeightEnabled: true,
            serverUrl: baseUrl + 'Track/uploadImage',
            textarea: 'XHTrack_Problem_sTaskContent',
            initialFrameWidth: '100%',
            imageScaleEnabled: false,
            toolbars: [[
                'source', '| ', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'strikethrough', '|', 'superscript', 'subscript', '|',
                'forecolor', 'backcolor', '|', 'removeformat',
                '|', 'insertorderedlist', 'insertunorderedlist', '|', 'selectall', 'cleardoc', 'paragraph' ,
                '|', 'justifyleft', 'justifycenter', 'justifyright',
                '|', 'insertimage', 'insertvideo', 'link', 'unlink',
                '|', 'horizontal', 'preview ',
            ]],

            /* 上传图片配置项 */
            "imageActionName": "uploadImage", /* 执行上传图片的action名称 */
            "imageFieldName": "upfile", /* 提交的图片表单名称 */
            "imageMaxSize": 2048000, /* 上传大小限制，单位B */
            "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
            "imageCompressEnable": true, /* 是否压缩图片,默认是true */
            "imageCompressBorder": 1600, /* 图片压缩最长边限制 */
            "imageInsertAlign": "none", /* 插入的图片浮动方式 */
            "imageUrlPrefix": "", /* 图片访问路径前缀 */
            "imagePathFormat": "",

            /* 上传视频配置 */
            "videoActionName": "uploadImage", /* 执行上传视频的action名称 */
            "videoFieldName": "upfile", /* 提交的视频表单名称 */
            "videoPathFormat": "", /* 上传保存路径,可以自定义保存路径和文件名格式 */
            "videoUrlPrefix": "", /* 视频访问路径前缀 */
            "videoMaxSize": 102400000, /* 上传大小限制，单位B，默认100MB */
            "videoAllowFiles": [
                ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上传视频格式显示 */
        });
    } else {
        var um = UM.getEditor('XHTrack_Task_sTaskContent', {
            autoHeightEnabled: true,
            imageUrl: baseUrl + 'Track/uploadImage',
            imagePath : '',
            textarea: 'XHTrack_Task_sTaskContent',
            imageScaleEnabled: false,
            dropFileEnabled: true,
            initialFrameWidth: '100%',
            toolbar: [
                'source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat',
                '| insertorderedlist insertunorderedlist | selectall cleardoc paragraph' ,
                '| justifyleft justifycenter justifyright ',
                '| image video  link unlink',
                '| horizontal preview ',
                '| formula'
            ]
        });
    }

    if (iTTId) {
        $.post(baseUrl + 'Track/getTaskInfo', {id:iTTId}, function(data){
            form.fill(data);
            // $("#xh_form").xhForm('fillData', data);
            // appPackDom.trigger('change').prop("disabled", true);
            appPackDom.trigger('change');
            proStyle.combotree('setValue',data.iProblemStyle);
            iReportUserId.combobox('setValue',data.iReportUserId);
            $('#XHTrack_Task_sSchoolId').val(data.sSchoolId ? data.sSchoolId.split(',') : '').trigger('change');//.prop("disabled", true);
            //超级管理员可修改应用
            if(taskType == 8){
                appPackDom.prop("disabled", false);
            }
            um.ready(function () {
                um.setContent(data.sTaskContent);
            });

        }, 'json');
    }

    // if (!useful.isTech && taskType !== '3') {
    //     $('#XHTrack_Task_sSolveVersion').parent().parent()
    //         .add($('#XHTrack_Task_tPlanFinishTime').parent().parent())
    //         .hide();
    // }
});
